﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LWJ_Tp_final.Models;
using WebMatrix.WebData;
using System.ComponentModel.DataAnnotations;


namespace LWJ_Tp_final_Administracion.Controllers
{
    public class UsuarioController : Controller
    {
        private DataContext BaseDeDatos = new DataContext();

        public ActionResult Index()
        {
            return View(BaseDeDatos.Usuarios.ToList());
        }

        //AGREGAR
        [HttpGet]
        public ActionResult Agregar()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Agregar(Registro usuario)
        {

            if (ModelState.IsValid)
            {
                if (WebSecurity.UserExists(usuario.UserName))
                {
                    ViewBag.LogError = true;
                    return View("Agregar", model: usuario);
                }
                WebSecurity.CreateUserAndAccount(usuario.UserName, usuario.Password);
                return RedirectToAction("Index");
            }
            else
            {
                ViewBag.Error = true;
                return View("Agregar", model: usuario);
            }

        }

    }

    //clase para agregar un usuario con username y password
    public class Registro
    {
        [Required]
        public string UserName { get; set; }
        [Required]
        [MinLength(5)]
        public string Password { get; set; }
    }
}


